Adding our houses and fences to our lots with code is a little bit harder than a tool because the tool
knows where to go.
So today we're going to add stuff with code and then we'll make the purchases.
Let's go ahead and play to see what we're going to do.
So I have four lots there.
I duplicated the other ones and I wrote a test script that's going to add a house and a fence to each
lot with code, right?
We're not buying it yet.
We're just doing this with code.
So we we're going to import the house.
We're going to import the fence so we can do it right.
That's pretty cool.
And there's a lot of useful stuff we're going to learn in this video.
All right.
So in case you didn't know where I got my house and my fence, I have my Roblox studio here.
That first starter place that they give you when you sign up, it's like mine, simtek nitros place.
And yours will be your character's names.
Place.
I got that here.
It's got a lot of cool stuff and you can use it, right?
You got beds, you got doors and you got building stuff.
I saved off this model right here.
I just, like, clicked on it and then I right click saved a Roblox, and then it'll be in my toolbox.
I did the same with the fence, but I modified the fence and then resaved it just so that you know.
But you don't have to do that for this video because what I did was I uploaded that stuff to the marketplace
under my account and I made it shareable.
So I'll put this link in a file called URLs.
That's in the resources.
You can go to it or you can just type it in if you see it there.
And this will say get model.
You go ahead and click it, then you'll get the cabin right?
But there's nothing special about it.
You can get it in your place too.
The cabin image you'll see that says images I saved off the image.
We're going to use that for our store, right?
We're going to have the image when we go to buy it.
So I save that off too.
And then we have the fence.
I modified the fence, so you might want to just get this one.
And then I have an image of that that I saved off for our store.
All of these will say, get or get model, something like that.
You go ahead and get them and then it'll be in your toolbox and you can follow along exactly in the
video.
Now let's get our fence.
So if you use the URL that I put in the resource file and then you went to the website and hit get model
for the fence, you can go to the toolbox and then get the fence.
And the image is going to be a little bit different because I did like five different images.
Let's go ahead and click it once.
Boom.
And this is going to be like way over there because.
You didn't make it.
But I did make it.
And I made it right here.
So I was going to be very close.
That's all right.
Just click on your fence and then drag it into place.
You may have to rotate it a little bit, depending on the orientation of your lot.
Is that good enough?
That's good enough.
I'll move it if I need to.
All right.
So we got our fence in place or somewhat in place?
And we're going to need an anchor point for our fence.
So unfortunately, everything here is called part.
You know what I'm going to do?
I'm just going to grab all these parts in this fence.
I'm going to make it into like a submodel.
I'll just right click and then group as model.
And that way I don't have to worry about all those parts because in the fence I'm going to hit the plus
sign and then I'm going to add a part and I want to be able to find it.
It's right here.
I'm going to call that fence anchor fence.
Let me move my mouse
core.
All right.
And like the lot anchor, we are going to need to know the front.
The front is pointing to the left.
I know, because that's the Z axis.
And the default is to add it to the negative facing down the negative Z axis.
But let's just be sure.
Let's add a decal.
The decal will default to the front of the part.
So it is facing the left.
We'll hit the fence anchor, we'll hit the rotate, go ahead and change the orientation of that part
so that it's facing the way we want for front.
And now on my fence.
Anchor I'm going to make it.
In the same position as my lot.
Anchor I'm just going to line them up with the code.
So let me go to my lot anchor and I'm going to get the position.
If I can find it.
There it is.
Position under the C frame control C go to my fence.
Anchor control V voila.
And we will make sure that it's still facing front.
It is right.
It shouldn't change the orientation, just the position.
All right.
Now we need to anchor the anchor.
So with the fence anchor selected in my fence model, I'm going to go down to.
Anchored and then in turn can collide off so they don't interfere with each other in the game and we
don't trip over it.
And I'm going to make it invisible.
So I'm going to go up to transparency, make a one.
Cool.
Now I got to get this fence model.
And then I'm going to go down to primary part.
Click that and I'm going to click Fence Anchor.
That's going to be my primary part for my fence.
Now let's grab our fence and I'm going to drag it down to I think I'll put it in server storage.
This will work in replicated storage or server storage.
But because it's fences and buildings and stuff like that, I think I'm going to go with server storage.
Let's get a script now.
Server script service.
What do we have in there?
I have my game manager, my property manager.
We're going to add another script.
Hit the plus sign script.
A regular one.
Not module, not local regular script.
And then let's call this property test.
Property test.
That's good enough.
We'll make this a little bigger so you can see it.
Now we're going to get a reference to our server storage for our fence.
So I'm going to say local SF for server storage game, get service server storage.
Let's make a variable for our fence.
Let's say local fence.
And this is the template.
We're going to clone the template over and over for all the different lots.
So fence temp SS, wait for child fence.
We'll do a wait statement so I can see it happen.
So the game will start up and I'll be in the world.
Then I'll do a print statement so that I know that the wait is over.
The wait is over.
And then we'll do a for loop for all of our different properties.
We're going to add a couple properties and we're going to orient them funny just to make sure the fence
lands in the right place.
So let's do for I and V in pairs workspace dot lots and we'll get children.
So that's all the different places do.
I don't know why that red keeps popping up.
There we go.
Went away.
Let's make a clone of the fence temp.
So I'm going to say local fence equals fence, temp, colon, clone.
And then we'll make the fence temps parent equal V, which is the lot.
Right?
So I is going to be one, two, three, four.
V is going to be the different lots that is in lots.
Right.
So the the player lots these that's a V, right.
So that's the parent.
And then I need the anchor of the lot.
So V dot anchor.
Yep, we call that anchor.
Okay good.
And now this is new, right?
And I used to use set primary part C frame, but now they're recommending we use the get pivot.
So I'm going to say pivot equals anchor colon, get pivot.
So it gets C frame information.
And I don't even think we have to do a move to.
I think that's going to be sufficient.
We'll find out a minute.
Let's get our fence now and we're going to say pivot to pivot.
So we're going to use the anchor of the lot and then the fence.
The primary part is the anchor point.
The anchor points pivoted in the same direction, same location, same direction as our fence.
We should have it plopped right down there in the lots.
So one of these isn't very good because we we lined up our lot.
Let's go to player lot.
Home collisions are off, so I'll do a control D to duplicate it.
Let's move that out and then let's just rotate it so that it's kind of funny rotation, right?
Maybe do another control D.
Oh, we get another lot here and we don't have to position them good.
We just want to make sure that things are different.
We're going to delete them.
We're going to delete them after this because we're not done this a lot.
But I want to see the fences appear.
Oh, let's go to view output window.
And we should see something happening.
There.
The wait is over and we have our fences.
That's pretty cool.
That fence looks good.
This fence looks good.
It's in the.
The openings in line with the for sale sign.
That looks good.
So we plop those fences down.
They're perfectly on our lots with code.
So maybe as an exercise, you could try it with the cabin.
I will do it in the next video, though.
But you might want to play around with it and see if you can get that working yourself.